package npr.gui;
import processing.core.*;


public class Slider extends GUI{

    private float start, end;
    private int pos;
    private float textPos;
    private int color;
    private int precision;
    private float offset;
    private float interval;
    private boolean move= false;
    private PFont numberFont;
    private int numberFontSize;
    Button button;
    Field field;
    float labelPos;
    String label;


    public Slider(int xpos, int ypos, int width, float start, float end, float init, int intervals, int precision, String label){
	this(xpos, ypos, width, 10, null, 10, start, end, init, intervals, precision, null, defaultFontSize, label);
     }

    public Slider(int xpos, int ypos, int width, int height, PFont numberFont, int numberFontSize, float start, float end, float init, int intervals, int precision, PFont font, int fontSize, String label){
  
	super(xpos, ypos, width, height, font, fontSize);
	this.offset= (float)width/(float)intervals;
	this.interval= ((float)(end-start)/(float)intervals);
	if(numberFont==null) this.numberFont= parent.loadFont("Verdana-10.vlw");
	else this.numberFont= numberFont;
	this.numberFontSize= numberFontSize;
	this.start= start;
	this.end= end;
	this.pos= (int)(((init-start)/(end-start))*(float)width);
	this.textPos= ((float)height-this.numberFont.ascent()*numberFontSize)/2.0f;
	this.color= 150;
	this.precision= precision;
	field= new Field(xpos, ypos, width, height);
	button= new Button(xpos+2, ypos+2, pos, height-4);
	this.labelPos= labelXPos(label);
        this.label= label;
    }


    public void draw(){

	if(display){
	    parent.hint(DISABLE_DEPTH_TEST);
	    parent.fill(fontColor);
	    parent.textFont(font, fontSize);
	    parent.text(label, xpos-labelPos, ypos+height+(5+numberFont.ascent()*numberFontSize)/2.0f);
	    
	    parent.fill(255);
	    if(enabled) parent.stroke(0);
	    else  parent.stroke(100);
	    parent.rect(xpos, ypos, width, height);
	    if(enabled) parent.fill(color);
	    else parent.fill(200);
	    parent.rect(xpos, ypos, pos, height);
	    
	    parent.fill(fontColor);
	    parent.stroke(fontColor);
	    parent.textFont(numberFont, numberFontSize);
	    
	    for(int i=0; i<=10; i++){
		parent.line(xpos+i*offset, ypos+height, xpos+i*offset, ypos+height+4);
		String label= toString(start+i*interval, precision);
		float textWidth=0;
		for(int j= 0; j<label.length(); j++){
		    textWidth+=numberFont.width(label.charAt(j))*10;
		}
		parent.text(label, xpos+i*offset-textWidth/2.0f, ypos+height+5+numberFont.ascent()*numberFontSize);
	    }
	    parent.hint(ENABLE_DEPTH_TEST);
	}
    }

    public static String toString(float value, int precision){

	String tmpStr = String.valueOf(value);
	int pos = tmpStr.indexOf(".");
	
	if(pos != -1 && pos+precision < tmpStr.length())
	    tmpStr = tmpStr.substring(0, pos+precision);
	
	return tmpStr;
    }

    public boolean inside(int x, int y){
	return x>xpos+pos-5 && x<xpos+pos+5 && y>ypos && y<ypos+height;
           
    }

  
    protected void mousePressed(int x, int y){
	if(inside(x, y)){
	    move= true;
	}
    }


    public void mouseDragged(int x, int y){
	if(move) pos= parent.constrain(x-xpos, 0, width);
    }


    public void mouseReleased(int x, int y){
	move= false;
    }


    public float getValue(){
	return start+(pos/(float)width)*(end-start);
    }

    public void setValue(float value){
	if(value >= start && value <= end){
	    this.pos= (int)(((value-start)/(end-start))*(float)width);
	}
    }
}

